package com.qiaoba.module.system.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.system.entity.SysDictData;
import com.qiaoba.module.system.entity.dto.SysDictDataDTO;
import com.qiaoba.module.system.entity.param.SysDictDataParam;
import com.qiaoba.module.system.service.SysDictDataService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 字典数据 Web层
 *
 * @author ailanyin
 * @date 2023-09-10 19:00:47
 */
@Validated
@RestController
@RequestMapping("/system/dict/data")
@RequiredArgsConstructor
@Tag(name = "字典数据")
public class SysDictDataController {

    private final SysDictDataService sysDictDataService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult<SysDictData> list(@Validated @ParameterObject SysDictDataParam param, @ParameterObject PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysDictData> list = sysDictDataService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    @Operation(summary = "获取详情")
    public Result<SysDictData> getInfo(@Size(max = 30, message = "dictCode不合法") @PathVariable String dictCode) {
        return Result.success(sysDictDataService.selectById(dictCode, false));
    }

    @GetMapping(value = "/type/{dictType}")
    @Operation(summary = "根据字典类型查询字典数据")
    public Result dictType(@Size(max = 30, message = "dictType不合法") @PathVariable String dictType) {
        return Result.success(sysDictDataService.selectFromCache(dictType));
    }

    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:dict:add')")
    @PostMapping
    @Operation(summary = "新增")
    public Result add(@Validated(AddGroup.class) @RequestBody SysDictDataDTO dto) {
        return Result.toAjax(sysDictDataService.insert(dto));
    }

    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:dict:edit')")
    @PutMapping
    @Operation(summary = "修改")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysDictDataDTO dto) {
        return Result.toAjax(sysDictDataService.update(dto));
    }

    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除")
    public Result remove(@PathVariable List<String> ids) {
        return Result.toAjax(sysDictDataService.deleteByIds(ids));
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:dict:export')")
    @PostMapping("/export")
    @Operation(summary = "导出Excel")
    public void export(HttpServletResponse response, @Validated SysDictDataParam param) {
        List<SysDictData> list = sysDictDataService.selectList(param);
        ExcelUtil.exportExcel(list, SysDictData.class, "字典数据", response);
    }
}
